package com.zzq.service.impl;

import com.zzq.domain.dto.SmileBlindboxGoodsDTO;
import com.zzq.domain.entity.SmileBlindboxGoods;
import com.zzq.domain.search.BlindboxGoodsSearchParamet;
import com.zzq.mapper.SmileBlindboxGoodsMapper;
import com.zzq.result.SmileResult;
import com.zzq.service.SmileBlindboxGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionalEventListener;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小布
 * @since 2023-11-06
 */
@Service
public class SmileBlindboxGoodsServiceImpl extends ServiceImpl<SmileBlindboxGoodsMapper, SmileBlindboxGoods> implements SmileBlindboxGoodsService {

    @Autowired
    SmileBlindboxGoodsMapper blindboxGoodsMapper;

    @Override
    public List<SmileBlindboxGoodsDTO> getSmileBlindboxGoodsDTOList(BlindboxGoodsSearchParamet searchParamet) {
        return blindboxGoodsMapper.getSmileBlindboxGoodsDTOList(searchParamet);
    }

    @Override
    public void updProbability() {
        List<SmileBlindboxGoods> blindboxGoodsList = blindboxGoodsMapper.selectList(null);
        Double weight = Double.valueOf(blindboxGoodsList.stream().mapToInt(SmileBlindboxGoods::getGoodsWeight).sum());
        for (SmileBlindboxGoods goods : blindboxGoodsList) {
            Double goodsWeight = Double.valueOf(goods.getGoodsWeight());
            BigDecimal probability=new BigDecimal(goodsWeight/weight);
            goods.setGoodsProbability(probability);
            blindboxGoodsMapper.updateById(goods);
        }
    }
}
